package com.xxx.idam.sample;

import org.apache.commons.logging.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value="/sample")
public class UserController {
	private static Log log = LogFactory.getLog(UserController.class);
	
	@Autowired
	private IUserService userServiceImpl;
	
	
	//private IUserDao userDao;

	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String List(ModelMap model){
		log.info("Sample.List");
		
		java.util.List listUsers = userServiceImpl.getUsers();

        model.addAttribute("listUsers", listUsers);
        return "Sample/List";
    }
	
	@RequestMapping(value="/detail", method=RequestMethod.GET)
	public String Detail(ModelMap model, @RequestParam("id") int id){
		log.info("Sample.Detail");
		
		UserDomain userDomain = userServiceImpl.getUser(id);
		
		UserModel user = new UserModel(userDomain);

		model.addAttribute("user", user);
		
		return "Sample/Detail";
	}
	
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String Add(ModelMap model)
	{
		log.info("Sample.Add");
		
		UserModel user = new UserModel();
        model.addAttribute("user", user);
        return "Sample/Add";
	}
	
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public String Edit(ModelMap model, @RequestParam("id") int id)
	{
		log.info("Sample.Edit");
		
		UserDomain userDomain = userServiceImpl.getUser(id);
		
		UserModel user = new UserModel(userDomain);

		model.addAttribute("user", user);

		return "Sample/Edit";
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String Save(ModelMap model, @ModelAttribute("user") UserModel user){
		log.info("Sample.Save");
		userServiceImpl.saveUser(user.getUserDomain());
		return "redirect:list.do";
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public String Remove(ModelMap model, @ModelAttribute("user") UserModel user){
		log.info("Sample.Remove");
		userServiceImpl.removeUser(user.getUserDomain().getId());
		return "redirect:list.do";
	}
	
}
